<?php

namespace Home\Controller;

use Home\Base\BaseController;

use Think\Controller;
use Think\Log;


//接口处
interface {{service}}{
    public function index(); //查
    public function add();   //增
    public function edit();  //改
    public function selectAction();//增Ajax请求
    public function addAction();//增Ajax请求
    public function editAction();
    public function findAction();//查找某一条
    public function delAction();
}


class {{controller}} extends BaseController implements {{service}}
{

    //查询页面
    public function index(){
        $this->display();
    }

    //查询Ajax请求
    public function selectAction(){
            {{search}}



            ${{BModel}}=M('{{model}}');
            ${{infoModel}}=${{BModel}}
                ->field("{{selectfield}}")
                {{join}}
                {{where}}
                {{searchWhere}}
                ->page($this->page,$this->limit)
                {{order}}
                ->select();

            $count=${{BModel}} {{where}}->count();

            Log::write(${{BModel}}->getLastSql());

            $this->AjaxSuccess(['{{infoModel}}'=>${{infoModel}},'count'=>$count]);
    }


    //添加页面
    public function add(){
        $this->display();
    }
    //添加Ajax请求
    public function addAction(){
        #code
        {{addcode}}

        ${{BModel}}=M('{{model}}');
            $isok=${{BModel}}
                ->data($addData)
                ->add();

            if(empty($isok)){
                $this->AjaxError("添加失败!");
            }else{
                $this->AjaxSuccess("添加成功!");
            }


    }

    //查询页面
    public function edit(){
        $this->display();
    }
    //查询Ajax请求
    public function editAction(){
        #code

        ${{table_key}}=I('{{table_key}}');

        {{addcode}}

        ${{BModel}}=M('{{model}}');
                $isok=${{BModel}}
                    ->data($addData)

                    ->where(['{{table_key}}'=>${{table_key}}])
                    ->save();

                if(empty($isok)){
                    $this->AjaxError("修改失败!");
                }else{
                    $this->AjaxSuccess("修改成功!");
                }

    }


    //删除Ajax
    public function delAction(){
        #code
        ${{table_key}}=I('{{table_key}}');
        $isok=M('{{model}}')
            ->where(['{{table_key}}'=>${{table_key}}])
                    ->delete();

        if(empty($isok)){
            $this->AjaxError("删除失败!");
        }else{
            $this->AjaxSuccess("删除成功!");
        }
    }
    //查询单个Ajax请求
    public function findAction(){
        #code

        ${{table_key}}=I('{{table_key}}');

        ${{BModel}}=M('{{model}}');

        ${{infoModel}}=${{BModel}}
                        ->where(['{{table_key}}'=>${{table_key}}])
                        ->find();

        $this->AjaxSuccess(${{infoModel}});
    }






}




